package com.wwl.cms.utils.designModeTest.observer.improve;

import lombok.Data;

import java.util.ArrayList;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/9/13 19:46
 * @description:
 */
@Data
public class WeatherData implements Subject {
    private float temperature;
    private float pressure;
    private float humidity;
    //观察者集合
    private ArrayList<Observer> observers;
    //加入新的第三方
    public WeatherData(){
        observers = new ArrayList<Observer>();
    }
    public void dataChange(){
        notifyObserver();
    }

    public void setData(float temperature,float pressure,float humidity){
        this.temperature= temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        dataChange();
    }

    @Override
    public void registerObserver(Observer o){
        observers.add(o);
    }
    @Override
    public void removeObserver(Observer o){
        if (observers.contains(o)){
            observers.remove(o);
        }
    }

    @Override
    public void notifyObserver() {
        for (int i=0;i<observers.size();i++){
            observers.get(i).update(this.temperature,this.pressure,this.humidity);
        }
    }
}
